<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by CodeTlan.
 * User: Ing. Miguel Salas
 * Date: 3/4/12
 * Time: 3:51 PM
 */
class CT_Controller extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        if (!$this->session->isLogged()) {
            echo json_encode(array("fatal" => "Los datos de su sesi&oacute;n han caducado. <br>Para continuar, es necesario que vuelva a autenticarse. Lamentamos el inconveniente."));
            exit;
        }
    }

    /*
     * @ Array: data
     */
    function _add($data,$table=false,$logs=true)
    {
        if($logs){
            $data['fecha_creacion']=date("Y-m-d");
            $data['usuario_creacion']=$this->session->getUsuario();
            $data['fecha_modificacion']=date("Y-m-d");
            $data['usuario_modificacion']=$this->session->getUsuario();
        }
        $this->dao->add($data,$table);
        return "{success:true}";
    }

    /*
    * @ Array: data,token
    */
    function _edit($data,$tokens,$table=false,$logs=true){
        if($logs){
            $data['fecha_modificacion']=date("Y-m-d");
            $data['usuario_modificacion']=$this->session->getUsuario();
        }
        $this->dao->edit($data, $tokens, $table);
        echo "{success:true}";
    }

    function getSuccess($result){
        $result["success"]=true;
        return json_encode($result);
    }

    function uploadfile(){
        $config=array(
            'file_name' => md5(date('Y-m-d H:i:s')),
            'upload_path' => 'uploads/',
            'allowed_types' => 'jpg',
            'max_size' => '10240'
        );
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload())
        {
            $return=array(
                "success" => false,
                "msg" => $this->upload->display_errors('','')
            );
        }
        else{
            $return=array(
                "success" => true,
                "data" => $this->upload->data()
            );
        }
        return $return;
    }
}